
$(function(){
	// 定时器
	var timer = "";
	var rTimer;
	var dataCount = 20; 
   	var htmlStr ="";
   	var navHStr ="";
   	var favaCounts = [];
	// 当页面滚动式，出现上滑到顶部和底部的按钮
	// 并且通知消息框消失
	var totalHeight = $("#data-content")[0].scrollHeight;
	
//	console.log(totalHeight);
//	console.log(height);
		$("#data-content").on("scroll",function(){	
//			alert(1)
			var sT = $("#data-content").scrollTop();
			var height = $("#data-content").height();
			var total = sT+height;
		
				clearTimeout(rTimer);
				timeOut();
				setTimeout(function(){
					$("#searchDiv ul").slideUp();
				},2000)			
				if(sT){
					clearTimeout(timer);
					$(".shortcutBtn").fadeIn();
					 timer = setTimeout(function(){
						$(".shortcutBtn").fadeOut();				
					},1000);	
				}

			if(total>=totalHeight-10){
//				sendFCs();
				$(".loading").css({backgroundImage:"url(../images/loading_010.gif)",backgroundRepeat:"no-repeat",backgroundPosition: "50% 0"}).find("div").hide();	
					$.ajax({
						type:"get",
						url:"/newData",
						async:true,
						data:dataCount
					}).done(function(resData){
						if(resData){							
							htmlStr = template("mainTemplate",resData);
   							$("#data-content").append(htmlStr);
   							$(".loading").appendTo("#data-content");
   							dataCount = dataCount+20;
   							totalHeight = $("#data-content")[0].scrollHeight;
   							$(".dataCount").text(dataCount);
							$(".loading").css("background-image","").find("div").show().find(".iconM").show().next().text("上拉加载"); 						
						}else{							
							$(".iconM").hide().next().text("没有更多的数据");
						}
					}).fail(function(jqXHR,status,error){
						console.log(error);
					$(".loading").css("background-image","").find("div").show().html("<p><span class='glyphicon glyphicon-remove'></span>&nbsp;加载失败</p>");					
						
					})
			}
			});
			
//			$("#data-content ul").on("click",function(event){
//				event.stopImmediatePropagation();
//				timeout();
//				clearTimeout(rTimer);
//			})
		// 回到顶部按钮事件 
		$("#backTop").click(function(event){
			$("#data-content").animate({scrollTop:0},500);

		});
		// 回到底部按钮事件
		$("#backBottom").click(function(){
			var vh = $(window).height();			
			$("#data-content").animate({scrollTop:vh},500);
		});
	// 顶部通知栏出现
	if($.cookie("userId")){
		$("#remind").stop().slideDown();
	}
	
	// timeOut 让顶部消息栏消失
	function timeOut(){		
		$("#remind").stop().slideUp();
		
	};
	// 5s 后 顶部消息栏消失
	function remindUp(){
		 rTimer = setTimeout(timeOut,5000);
	}
	remindUp();
	// 点击顶部的消息按钮，出现或消失 顶部消息栏
	$(".titleMessage").on("click",function(){
		$("#remind").stop().slideToggle();		
		remindUp();
	});	
	// 
	setTimeout(function(){
   		$("#nav").fadeIn(1000); 		
   	},2000)
	// 调节底部 分页栏位置 和顶部通知栏的位置
   	function navPosition(){
   		var wx = $(window).width() / 2 - 59;
   		var bx = $(window).width() / 2 + 20;
   		$("#nav").css("left",wx);
   		$(".dataCount").css("left",bx);
   	}
   	navPosition();
   	$(window).resize(function(){
   		navPosition();
   		totalHeight = $("#data-content")[0].scrollHeight;
   	});
   	
   	var index = 0;
   	// 侧边栏的一级列表事件
   $(".navMenu").on("click",".listOnce",function(){ 	
   	if(!$(this).data("open")){
   		$(this).find("span").removeClass("glyphicon-triangle-right").addClass(" glyphicon-triangle-bottom").end().next().slideDown();
   		$(this).data("open",true);
   	}else{
   		$(this).find("span").removeClass(" glyphicon-triangle-bottom").addClass("glyphicon-triangle-right ").end().next().slideUp();
   		$(this).data("open",false);  		
   	} 		
   }); 	
   	// 点击汉堡包菜单 弹出侧边栏
   	$(".menuButton").click(function(){
   		clearTimeout(rTimer);
   		timeOut();  
   			$("body").css("overflow","hidden");  		
   		var random = Math.floor(Math.random()*5);
   		var colors = ["lightblue","lightcoral","lightgrey","lightpink","lightgoldenrodyellow"];
   			$(".guard").slideDown();
   			$(".navMenu").css("background-color",colors[random]).show(1000);
   	});
   	// 点击侧边栏关闭按钮 关闭侧边栏
   	function closeLNav(){
   		$(".guard").slideUp();
   		$(".navMenu").hide(1000);
   		$("body").css("overflow","scroll"); 
   	}
   	$(".closeLeftMenu").click(function(){
   		 		closeLNav();  		
   	})
   	//
   	
   
   	
   	/*************请求*************/
 
   	$.ajax({
   		type:"post",
   		url:"/index",
   		async:true
   	}).done(function(data,status,jqXHR){
   		if(status == 400){
   			// 顶部通知栏最新数据
   			$("#newReply").text(data.newReply);
   			$("#newHistory").text(data.newHistory);
   			$("#newSubscribe").text(data.newSubscribe);
   			$(".title").text(data.title);
   			 htmlStr = template("mainTemplate",data.section);
   			$("#data-content").html(htmlStr);
   			 navHStr = template("leftNavTemplate",data.leftNav);
   			$("#navContainer").html(navHStr);
   		}
   	}).fail(function(jqXHR,status,error){
   		console.log(error);
   	});
 
  
	$("#data-content").on("click",".moveTheme",function(){
		$.get("/moveTheme",$(this).find(".sectionID").text());
	});
   	
   	$("#searchSection").on("input",function(){
   		var  searchStr = $(this).val();
   		if(!searchStr){
   			$("#navContainer").html(navHStr);  			 
   		}else{
   			$(".titleOnce").each(function(index,value){
   				if($(this).text().includes(searchStr)){
					var htmlStr = "<li class='list-group-item listOnce'>";
					htmlStr += $(this).parent().html();
					htmlStr += "</li><ul class='list-group listSecond'>";
					htmlStr += $(this).parent().next().html();
					htmlStr += "</ul>";
					$("#navContainer").html(htmlStr);
   				}
   			});
   		}		
   	});
   
   $(".navMenu").on("click",".theme",function(){
   		var val  = $(this).val(); 		
   		$.post("/searchTheme",val,function(){});
   		closeLNav(); 
   });
   
//function sendFCs(){	
//	$(".likeWC").each(function(){
//		if($(this).data("favaCount")){
//			var sId= $(this).parents("li").find(".sectionID").text();
//			var obj = {sId,favaCount:$(this).data("favaCount")}
//			favaCounts.push(obj);
//		}
//	});
//	if(favaCounts.length){
//		$.post("/favaCount",favaCounts);
//	}
//}
});
//window.onbeforeunload = function(){
////	alert(1);
//	sendFCs();
//}		